const ajax=(type,path,data={},dataType="json")=>{

   const p=new Promise(resolve=>{
       let params=""
       for(let key in data){
         params +=`${key}=${data[key]}&`
       }
       params=params.substring(0,params.length-1)

       const xhr=new XMLHttpRequest()
       if(type.toLowerCase()=="get"){
       xhr.open(type,path+"?"+params,true)
       xhr.send()
      }else if(type.toLowerCase()=="post"){
        xhr.open(type,path,true)
        xhr.setRequestHeader("content-type","application/x-www-form-urlencoded")
        xhr.send(params)
      }

      xhr.onreadystatechange=function(){
        if(xhr.readyState==4&&xhr.status==200){
          let data=xhr.responseText
          if(dataType.toLowerCase()=="json"){
            data=JSON.parse(data)     
          }     
          resolve(data)
        }
      }
   })

  return p
}